package com.ruoyi.operator.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.operator.constant.MemberConstants;
import com.ruoyi.operator.mapper.OptShopMemberMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.operator.domain.OptShopMember;
import com.ruoyi.operator.service.IOptShopMemberService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户会员Controller
 * 
 * @author zhanghang
 * @date 2021-01-20
 */
@Controller
@RequestMapping("/operator/member")
public class OptShopMemberController extends BaseController {
    private String prefix = "operator/member";

    @Autowired
    private IOptShopMemberService optShopMemberService;

    @RequiresPermissions("operator:member:view")
    @GetMapping()
    public String member() {
        return prefix + "/member";
    }

    /**
     * 查询商户会员列表
     */
    @RequiresPermissions("operator:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OptShopMember optShopMember) {
        startPage();
        optShopMember.setIsDelete("0");
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if(!(currentUser.getDeptId() == 100)){ optShopMember.setDeptId(currentUser.getDeptId()); }
        List<OptShopMember> list = optShopMemberService.selectOptShopMemberList(optShopMember);
        return getDataTable(list);
    }


    /**
     * 查询商户会员列表
     */
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(OptShopMember optShopMember) {
        startPage();
        optShopMember.setIsDelete("0");
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        optShopMember.setDeptId(currentUser.getDeptId());
        List<OptShopMember> list = optShopMemberService.selectOptShopMemberList(optShopMember);
        return getDataTable(list);
    }

    /**
     * 导出商户会员列表
     */
    @RequiresPermissions("operator:member:export")
    @Log(title = "商户会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OptShopMember optShopMember)
    {
        List<OptShopMember> list = optShopMemberService.selectOptShopMemberList(optShopMember);
        ExcelUtil<OptShopMember> util = new ExcelUtil<OptShopMember>(OptShopMember.class);
        return util.exportExcel(list, "member");
    }

    /**
     * 新增商户会员
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商户会员
     */
    @RequiresPermissions("operator:member:add")
    @Log(title = "商户会员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OptShopMember optShopMember) {
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        optShopMember.setDeptId(currentUser.getDept().getDeptId());
        int count = optShopMemberService.SelectMobileNumber(optShopMember);
        if(count>0){ return error("该手机号码已注册!"); }
        return toAjax(optShopMemberService.insertOptShopMember(optShopMember));
    }

    /**
     * 修改商户会员
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OptShopMember optShopMember = optShopMemberService.selectOptShopMemberById(id);
        mmap.put("optShopMember", optShopMember);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户会员
     */
    @RequiresPermissions("operator:member:edit")
    @Log(title = "商户会员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OptShopMember optShopMember)
    {
        return toAjax(optShopMemberService.updateOptShopMemberinfo(optShopMember));
    }


    /**
     * 预览商户会员
     */
    @RequiresPermissions("operator:member:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        OptShopMember optShopMember = optShopMemberService.selectOptShopMemberById(id);
        mmap.put("optShopMember", optShopMember);
        return prefix + "/detail";
    }

    /**
     * 查询商户会员余额
     */
    @RequiresPermissions("operator:member:detail")
    @PostMapping("/balance")
    @ResponseBody
    public OptShopMember balance(OptShopMember member)
    {
        OptShopMember optShopMember = optShopMemberService.selectMemberByCardNumber(member.getMemberCardNumber());
//        Map<String, Object> result = new HashMap<>();
//        result.put("balance", optShopMember.getBalance());
//        result.put("principalBalance", optShopMember.getPrincipalBalance());
//        result.put("activityBalance", optShopMember.getActivityBalance());
        return optShopMember;
    }



    /**
     * 删除商户会员
     */
    @RequiresPermissions("operator:member:remove")
    @Log(title = "商户会员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        OptShopMember optShopMember = new OptShopMember();
        optShopMember.setId(Long.parseLong(ids));
        if (MemberConstants.MEMBER_BALANCE_NOT_ZERO.equals(optShopMemberService.checkBalanceIsZero(optShopMember))) {
            return error("该用户账户余额不为0,退卡后才能删除");
        }
        optShopMember.setIsDelete("1");
        return toAjax(optShopMemberService.updateByIds(optShopMember));
    }


    /**
     * 选择部门会员树
     *
     */
    @GetMapping(value = { "/selectMemberTree" })
    public String selectDeptTree(OptShopMember optShopMember)
    {
        return prefix + "/tree";
    }


    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(OptShopMember optShopMember)
    {

        return optShopMemberService.checkPhoneUnique(optShopMember);
    }


}
